<?php
/**
 * Created by PhpStorm.
 * User: Do
 * Date: 7/2/14
 * Time: 11:39 AM
 */
    class Buzzy {
        private $url = "index";
        private $size = 0;
        private $router = array();
        function __construct() {
            if (isset($_GET["url"]))
                $this->url = $_GET["url"];
            $this->url = rtrim($this->url, '/');
            //Call router
            require_once ROOT_PATH.'libs/router.php';
            $this->router = ( ! isset($router) OR ! is_array($router)) ? array() : $router;
            foreach ($this->router as $key => $val){
                // Convert wild-cards to RegEx
                $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));

                // Does the RegEx match?
                if (preg_match('#^'.$key.'$#', $this->url))
                {
                    // Do we have a back-reference?
                    if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
                    {
                        $val = preg_replace('#^'.$key.'$#', $val, $this->url);
                    }
                    //reset url
                    $this->url = $val;
                }
            }

            $this->url = explode('/', $this->url);
            $this->size = sizeof($this->url);
            //File controller
            $file = "controllers/" . $this->url[0] . '.php';
            if (file_exists($file)) {
                require $file;
            } else {
                $this->errorFunction();
            }
            //Call class in controller file
            $controller = new $this->url[0]();

            //Call function in class
            if (isset($this->url[1])) {
                $function = $this->url[1];
                if(method_exists($controller, $function)){
                    //function exist and set parameter for function (function have 1,2,3,...parameter)
                    if (isset($this->url[2])) {
                        //parameter
                        $parameter = array();
                        for($i = 2; $i <= $this->size; $i++){
                            if(isset($this->url[$i])) {
                                array_push($parameter, $this->url[$i]);
                            }
                        }
                        call_user_func_array(array($controller, $function), $parameter);
                        //$controller->$function($this->url[2]);
                    }else{
                        $controller->$function();
                    }
                }else{
                    //if function do not exists show function index(default) and set parameter (have 1,2,3,...parameter)
                    $parameter = array();
                    for($i = 1; $i <= $this->size; $i++){
                        if(isset($this->url[$i])){
                            array_push($parameter, $this->url[$i]);
                        }
                    }
                    call_user_func_array(array($controller, "index"), $parameter);
                    //$controller->index($this->url[1]);
                }

            } else {
                $controller->index();
            }
        }

        function errorFunction() {
            require_once ROOT_URL.'controllers/error.php';
            $controller = new Error();
            $controller->index();
            exit;
        }
    }
?>
